package drone.basis.util ;

import java.util.HashMap ;
import java.util.Map ;

import com.alibaba.fastjson.JSONArray ;
import com.alibaba.fastjson.JSONObject ;

import drone.basis.dictionary.CountyDict ;

public class WeatherUtil
{

	private static String host = "https://qryweather.market.alicloudapi.com" ;

	private static String recentPath = host + "/lundroid/queryweather" ;

	private static String areaPath = host + "/lundroid/queryareaid" ;

	private static String appCode = "503b01609b4a4ce7ac493fba025586c2" ;

	private static String appKey = "203968448" ;

	private static String appSecret = "NhAv1OnGvIMD55Bjtxra4CbBLkNmUNQh" ;

	public static String recent (
			String county
	)
	{

		JSONObject result = new JSONObject ( ) ;

		String areaid = StringUtil.EMPTY ;

		try
		{
			areaid = area ( CountyDict.cn ( county ) ) ;

		} catch ( Exception e )
		{
			return StringUtil.EMPTY ;
		}

		if ( StringUtil.isEmpty ( areaid ) )
		{
			return StringUtil.EMPTY ;
		}

		Map < String , String > headerMap = new HashMap < String , String > ( ) ;
		headerMap.put ( "Authorization" , "APPCODE " + appCode ) ;
		headerMap.put ( "appKey" , appKey ) ;
		headerMap.put ( "appSecret" , appSecret ) ;
		Map < String , String > paramsMap = new HashMap < String , String > ( ) ;
		paramsMap.put ( "areaid" , areaid ) ;

		try
		{
			String response = HttpUtil.get ( recentPath , paramsMap , headerMap ) ;

			JSONObject data = JSONObject.parseObject ( response ).getJSONObject ( "data" ) ;

			JSONObject now = data.getJSONObject ( "info" ).getJSONObject ( "now" ) ;

			result.put ( "province" , data.getString ( "province" ) ) ;
			result.put ( "city" , data.getString ( "city" ) ) ;
			result.put ( "area" , data.getString ( "area" ) ) ;

			result.put ( "direction" , now.getString ( "wind_direction" ) ) ;
			result.put ( "power" , now.getString ( "wind_power" ) ) ;
			result.put ( "picture" , now.getString ( "weather_pic" ) ) ;
			result.put ( "weather" , now.getString ( "weather" ) ) ;
			result.put ( "temperature" , now.getString ( "temperature" ) ) ;
			result.put ( "humidity" , now.getString ( "sd" ) ) ;

			return normal ( result ) ;

		} catch ( Exception e )
		{
			System.err.println ( "can't fetch weather" ) ;
		}

		return StringUtil.EMPTY ;

	}

	public static String simplify (
			JSONObject weather
	)
	{

		return "天气情况 : " + weather.getString ( "weather" ) ;
	}

	public static String normal (
			JSONObject weather
	)
	{

		return weather.getString ( "area" ) + " 气温 : " + weather.getString ( "temperature" ) + " ℃ 天气情况 : " + weather.getString ( "weather" ) ;
	}

	public static String advance (
			JSONObject weather
	)
	{

		return weather.getString ( "province" ) + " " + weather.getString ( "city" ) + " " + weather.getString ( "area" ) + " 风向 : " + weather.getString ( "direction" ) + " 风力 : " + weather.getString ( "power" ) + " 湿度 : "
				+ weather.getString ( "humidity" ) + " 气温 : " + weather.getString ( "temperature" ) + " ℃ 天气情况 : " + weather.getString ( "weather" ) ;
	}

	public static String area (
			String areaName
	) throws Exception
	{

		Map < String , String > headerMap = new HashMap < String , String > ( ) ;
		headerMap.put ( "Authorization" , "APPCODE " + appCode ) ;
		Map < String , String > paramsMap = new HashMap < String , String > ( ) ;
		paramsMap.put ( "area" , areaName ) ;

		try
		{
			String response = HttpUtil.get ( areaPath , paramsMap , headerMap ) ;
			JSONObject result = JSONObject.parseObject ( response ) ;
			JSONArray list = result.getJSONObject ( "data" ).getJSONArray ( "lists" ) ;
			if ( list.size ( ) > 0 )
			{
				JSONObject areaObject = list.getJSONObject ( 0 ) ;
				return areaObject.getString ( "areaid" ) ;
			}
		} catch ( Exception e )
		{
			throw e ;
		}

		return StringUtil.EMPTY ;

	}

	public static void main (
			String [ ] args
	)
	{
		System.out.println ( recent ( "qw" ) ) ;
	}
}
